package kr.ac.hanyang.cs.park;

import java.io.InputStream;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.*;
import android.util.Log;

public class AddrInfo extends OptionInfo {
	private String name										= "";
	private String option									= "";		// need Phone Number to call
	private boolean isDebug									= false;
	private Context context									= null;
	private Drawable icon									= null;

	

	public AddrInfo(Context context) {
		this.context										= context;
	}


	public void setDebug(boolean isDebug) {
		this.isDebug										= isDebug;
	}


	private void prettyPrint() {
		Log.d("AddrList ", icon + " || " + name + " || " + option + " || ");
	}


	public ArrayList<OptionInfo> getAddressInfo() {
		ArrayList<OptionInfo> returnList					= new ArrayList<OptionInfo>();
		
		String projection[]									= new String[] {
																ContactsContract.Data.CONTACT_ID
																,ContactsContract.Data.DISPLAY_NAME
																,ContactsContract.Data.PHOTO_ID
																,ContactsContract.Data.MIMETYPE
																,ContactsContract.Data.DATA2	// type
																,ContactsContract.Data.DATA1	// phone.number, organization.company
															};

		Cursor cursor										= context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
																									,projection
																									,ContactsContract.Data.MIMETYPE +"='"+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE +"' or " +ContactsContract.Data.MIMETYPE +"='"+ ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE +"'"
																									,null
																									,ContactsContract.Data.DISPLAY_NAME+","+ContactsContract.Data._ID+" COLLATE LOCALIZED ASC");

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			OptionInfo aItem								= new OptionInfo();
			String addrName									= cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
			String addrPhone								= cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
			Bitmap iconBitmap								= BitmapFactory.decodeStream(Contacts.openContactPhotoInputStream(context.getContentResolver(), ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)))));
			if (iconBitmap == null)
				iconBitmap									= BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_menu_report_image);
			@SuppressWarnings("deprecation")
			Drawable addrIcon								= new BitmapDrawable(iconBitmap);
			
			aItem.setIcon(addrIcon);
			aItem.setName(addrName);
			aItem.setOption(addrPhone);

			returnList.add(aItem);

			cursor.moveToNext();
		}

		cursor.close();

		return returnList;
	}



	public String getName() {
		return name;
	}

	public String getOption() {
		return option;
	}

	public Drawable getIcon() {
		return icon;
	}


}
